#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File    : microbiome_group_info.py
@Author  : Bing Liang
@Email   : believer19940901@gmail.com
@Date    : 2025/10/12 22:07
@Description : 从配置文件中读取分组信息
"""
from argparse import  Namespace, ArgumentParser
from pathlib import Path
import json


def main(args: Namespace):
    input_path = Path(args.input).resolve()
    output_path = Path(args.output).resolve()
    output_path.parent.mkdir(parents=True, exist_ok=True)
    with input_path.open("r", encoding="utf-8") as fr:
        config_dict: dict = json.load(fr)
    with output_path.open("w", encoding="utf-8") as fw:
        fw.write("Sample\tGroup\n")
        for group in config_dict["groups"]:
            for sample in config_dict["groups"][group]:
                if sample in config_dict["samples"]:
                    fw.write(f"{sample}\t{group}\n")


if __name__ == '__main__':
    parser = ArgumentParser(description='从配置信息中获取分组信息')
    parser.add_argument('-i', '--input', required=True, type=str, help='配置文件')
    parser.add_argument('-o', '--output', required=True, type=str, help='分组信息')
    arguments = parser.parse_args()
    main(arguments)
